// 导入 ComponentWithStore 方法
import { ComponentWithStore } from 'mobx-miniprogram-bindings'
// 导入 store 对象
import { userStore } from '@/store/userstore'
import { updateCustom } from '@/api/custom'
import { uploadFile } from '@/api/system'
import { confirmModel } from '@/utils/extendApi'
import { toast } from '@/utils/extendApi'
ComponentWithStore({
  // 让页面和 Store 对象建立关联
  storeBindings: {
    store: userStore,
    fields: ['userInfo'],
    actions: ['setUserInfo','getToken']
  },
  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    user: {},
    token:''
  },

  /**
   * 组件的方法列表
   */
  methods: {
    chooseAvatar(event: any) {
      const { avatarUrl } = event.detail
      const userInfo = this.data.userInfo
      uploadFile(avatarUrl).then(res => {
        userInfo.avatar = res.data
        this.setUserInfo(userInfo)
        updateCustom(userInfo)
      })
    },
    async updateNick() {
      const data = await confirmModel({
        editable: true, placeholderText: "输入昵称", title: "修改昵称",
        content: this.data.userInfo.name,
      })
      if(data && data.isConfirm){
        if(data.content.length>15){
          toast({ title: '长度超过十五个字符' })
          return;
        }
        const userInfo = this.data.userInfo
        userInfo.name = data.content
        this.setUserInfo(userInfo)
        updateCustom(userInfo)
      }
    }
  },
  lifetimes:{
    attached(){
      this.setData({
        token:this.getToken()
      })
      
    }
  }
})